package service

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
)

func Customer(r *gin.Engine) {
	customer := r.Group("/customer")
	{
		customer.POST(sql.RequestAdd, customerAdd)
		customer.POST(sql.RequestUpdate, CustomerUpdate)
	}
}

func CustomerList(c *gin.Context) {
	sql.HttpList(c, &[]model.Customer{}, func(db *gorm.DB) *gorm.DB {
		search := c.Query("search")
		if search != "" {
			db = db.Where("name LIKE ?", "%"+search+"%").
				Or("phone_one LIKE ?", "%"+search+"%")
		}
		return db
	})
}

func CustomerDetail(c *gin.Context) {
	sql.DBHandle(c, "", func(context *gin.Context, db *gorm.DB) interface{} {
		var customer model.Customer
		id := c.Query("id")
		if id == "" {
			return nil
		}
		db.Debug().
			Where("id = ?", id).
			Preload("CompanySalesRecord").
			Preload("CompanySalesRecord.ProductSalesRecord").
			Preload("CompanySalesRecord.ProductSalesRecord.InventoryList").
			First(&customer)

		var customerDetail model.CustomerDetailResult
		customerDetail.ID = customer.ID
		customerDetail.UserId = customer.UserId
		customerDetail.Avatar = customer.Avatar
		customerDetail.Name = customer.Name
		customerDetail.Phone = customer.PhoneOne
		customerDetail.Company = customer.Company
		customerDetail.City = customer.City
		customerDetail.Address = customer.Address
		customerDetail.Remark = customer.Remark
		var salesList []model.ProductSalesResult
		for _, record := range customer.CompanySalesRecord {
			var salesResult model.ProductSalesResult
			salesResult.Id = record.ID
			salesResult.Product = record.ProductSalesRecord[0].Product
			salesResult.FactoryNo = record.ProductSalesRecord[0].InventoryList[0].FactoryNo
			salesResult.ProductName = record.ProductSalesRecord[0].InventoryList[0].ProductName
			salesResult.SalesNumber = record.ProductSalesRecord[0].SalesNumber
			salesResult.SalesWeight = record.ProductSalesRecord[0].SalesWeight
			salesResult.SalesPrice = record.ProductSalesRecord[0].SalesPrice
			salesResult.SalesDate = record.ProductSalesRecord[0].Date[0:10]
			salesList = append(salesList, salesResult)
		}
		customerDetail.SalesList = salesList
		return customerDetail
	})
}

func customerAdd(c *gin.Context) {
	sql.HttpAdd(c, &model.Customer{}, nil)
}

func CustomerUpdate(c *gin.Context) {
	sql.HttpUpdates(c, &model.Customer{}, nil)
}

func CustomerDelete(c *gin.Context) {
	sql.DBHandleDelete(c, model.Customer{})
	/*sql.DBHandle(c, sql.DELETE, func(context *gin.Context, db *gorm.DB) interface{} {
		id := c.Query("id")
		db.Where("id = ?", id).Delete(&model.Customer{})
		if db.RowsAffected > 0 {
			return nil
		}
		c.JSON(http.StatusOK, &model.BaseJsonBean{
			Code: 200, Message: sql.DELETE + "失败",
		})
		return nil
	})*/
}

func customerEdit(text string, c *gin.Context) {

	sql.DBHandle(c, text, func(context *gin.Context, db *gorm.DB) interface{} {
		var customer model.Customer
		if err := c.Bind(&customer); err != nil {
			return sql.RequestError(c)
		}
		if customer.Name == "" {
			return sql.ParamError(c, "客户名不能为空")
		} else if customer.PhoneOne == "" {
			return sql.ParamError(c, "客户电话不能为空")
		}

		if text == sql.ADD {
			db = db.Create(&customer)
		} else if text == sql.UPDATE {
			db = db.Updates(&customer)
		}
		if db.RowsAffected > 0 {
			return customer
		}
		c.JSON(http.StatusOK, &model.BaseJsonBean{
			Code: 200, Message: text + "失败",
		})
		return nil
	})
}
